package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.CoursesCardMapper;
import com.woniu.pojo.Card;
import com.woniu.mapper.DetailedOrderMapper;
import com.woniu.mapper.MemberCardMapper;
import com.woniu.mapper.OrderMapper;
import com.woniu.pojo.*;
import com.woniu.mapper.CardMapper;
import com.woniu.pojo.CoursesCard;
import com.woniu.service.ICardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.BuyCardVo;
import com.woniu.vo.CardSearchVo;
import com.woniu.vo.QueryCardVo;
import com.woniu.vo.cardVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
<<<<<<< HEAD
 * @author xie
=======
 * @author zbk
>>>>>>> 81444e4a262d317c7f42f747d976efc38ad8d785
 * @since 2022-10-12
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements ICardService {

    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CoursesCardMapper coursesCardMapper;

    @Autowired
    private HttpSession session;

    @Autowired
    private MemberCardMapper memberCardMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DetailedOrderMapper detailedOrderMapper;

    /**
     * 根据团课id查询会员卡
     * @param leagueId
     * @return
     */
    public List<CardSearchVo> queryByLeagueId(String leagueId) {
        return cardMapper.selectByLeagueId(leagueId);
    }

    @Override
    public List<cardVo> byCardType() {
        List<cardVo> list = cardMapper.byCardType();
        return list;
    }

    @Override
    public List<Card> queryByMemberId(String memberId) {
        return cardMapper.selectByMemberId(memberId);
    }

    @Override
    public List<Card> byTimetableId(String id) {
        QueryWrapper<CoursesCard> wrapper = new QueryWrapper<>();
        wrapper.eq("timetable_id",id);
        List<CoursesCard> coursesCards = coursesCardMapper.selectList(wrapper);
        Collection ids = new HashSet();
        if (coursesCards != null){
            for (CoursesCard coursesCard : coursesCards) {
                String cardId = coursesCard.getCardId();
                ids.add(cardId);
            }
        }
        List<Card> cardList = cardMapper.selectBatchIds(ids);
        return cardList;
    }

    @Override
    public List<BuyCardVo> queryAllBuy() {
        return cardMapper.selectAllBuy();
    }

    public BuyCardVo queryBuyById(String id) {
        BuyCardVo vo = cardMapper.selectBuyById(id);
        BigDecimal discount =(BigDecimal)session.getAttribute("discount");
        BigDecimal price = vo.getPrice();
        System.out.println(vo.getPrice());
        System.out.println(discount);
        BigDecimal realMoney=price.multiply(discount);
        vo.setRealMoney(realMoney);
        return vo;
    }

    @Override
    public int buyCard(BuyCardVo vo) {
        String tel = (String) session.getAttribute("tel");
        Member member = (Member) session.getAttribute("member");
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("member_id",member.getId());
        wrapper.eq("card_id",vo.getId());
        List<MemberCard> list = memberCardMapper.selectList(wrapper);
        if(list==null||list.size()==0){
            MemberCard memberCard=new MemberCard();
            memberCard.setAccount(vo.getAvailable());
            memberCard.setCardId(vo.getId());
            memberCard.setMemberId(member.getId());
            int insert = memberCardMapper.insert(memberCard);
        }else{
            for (MemberCard memberCard : list) {
                if(memberCard.getCardId().equals(vo.getId())){
                    memberCard.setAccount(memberCard.getAccount()+vo.getAvailable());
                    int insert = memberCardMapper.updateById(memberCard);
                }
            }
        }

        Order order=new Order();
        order.setOrderStatus("1");
        order.setOrderTotalMoney(vo.getRealMoney().doubleValue());
        int random = new Random().nextInt(10);
        order.setOrderNum(random +"");
        orderMapper.insert(order);
        DetailedOrder detailedOrder=new DetailedOrder();
        detailedOrder.setId(order.getId());
        detailedOrder.setOrderId(order.getId());
        detailedOrder.setMemberId(member.getId());
        detailedOrder.setGoodsId(vo.getId());
        int insert = detailedOrderMapper.insert(detailedOrder);
        return insert;
    }



    @Override
    public PageResult queryByItemInPage(long currentPage, long pageSize, Card card) {
        IPage page=new Page(currentPage,pageSize);
        IPage result=cardMapper.selectByItemInPage(page,card);
        return new PageResult(result.getTotal(),result.getRecords());
    }
}
